package com.dianneng.show.activity;

import android.os.Bundle;

import com.dianneng.show.R;
import com.dianneng.show.base.BaseActivity;
import com.dianneng.show.widget.DayValueFormatter;
import com.dianneng.show.widget.XYMarkerView;
import com.dianneng.show.widget.YValueFormatter;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {

    @BindView(R.id.bc_dianliang)
    BarChart bcDianliang;
    @BindView(R.id.lc_dianliang)
    LineChart lcDianliang;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initBarChart();
        initLineChart();
    }

    /**
     * 初始化柱形图控件属性
     */
    private void initBarChart() {
        // 设置x轴的LimitLine，index是从0开始的


        bcDianliang.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, Highlight h) {
                bcDianliang.getXAxis().removeAllLimitLines();
                LimitLine xLimitLine = new LimitLine(e.getX(),"");
                xLimitLine.setLineColor(getResources().getColor(R.color.colorPrimary));
                xLimitLine.setTextColor(getResources().getColor(R.color.colorPrimary));
                bcDianliang.getXAxis().addLimitLine(xLimitLine);
            }

            @Override
            public void onNothingSelected() {
                bcDianliang.getXAxis().removeAllLimitLines();
            }
        });
        bcDianliang.getDescription().setEnabled(false);
//        Description description=new Description();
//        description.setText("月用电量");
//        bcDianliang.setDescription(description);
        bcDianliang.setDoubleTapToZoomEnabled(false);
        bcDianliang.setScaleEnabled(false);
        bcDianliang.setMaxVisibleValueCount(32);
        bcDianliang.setDrawValueAboveBar(true);


        ValueFormatter dayValueFormatter=new DayValueFormatter();
        XAxis xAxis = bcDianliang.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f);
        xAxis.setLabelCount(25, false);
        xAxis.setValueFormatter(dayValueFormatter);


        //获取到图形左边的Y轴
        ValueFormatter yValueFormatter=new YValueFormatter();
        YAxis leftAxis = bcDianliang.getAxisLeft();
        leftAxis.setLabelCount(6, false);
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setSpaceTop(16f);
        leftAxis.setAxisMinimum(0f);
        leftAxis.setValueFormatter(yValueFormatter);

        //获取到图形右边的Y轴，并设置为不显示
        bcDianliang.getAxisRight().setEnabled(false);

        //图例设置
        Legend legend = bcDianliang.getLegend();
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setDrawInside(false);

        XYMarkerView xyMarkerView=new XYMarkerView(this);
        xyMarkerView.setChartView(bcDianliang);
        bcDianliang.setMarker(xyMarkerView);
        setBarChartData();
    }

    private void setBarChartData() {

        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();


        //在这里设置自己的数据源,BarEntry 只接收float的参数，
        //图形横纵坐标默认为float形式，如果想展示文字形式，需要自定义适配器，

        for (int i = 1; i < 32; i++) {
            yVals1.add(new BarEntry(i, (float) Math.random()*20));
        }
        BarDataSet set1;
        if (bcDianliang.getData() != null &&
                bcDianliang.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet) bcDianliang.getData().getDataSetByIndex(0);
            set1.setValues(yVals1);
            bcDianliang.getData().notifyDataChanged();
            bcDianliang.notifyDataSetChanged();
        } else {
            set1 = new BarDataSet(yVals1, "2019年7月");
            set1.setDrawIcons(false);
            set1.setColor(getResources().getColor(R.color.colorAccent));
            set1.setHighLightAlpha(255);
            set1.setHighLightColor(getResources().getColor(R.color.colorPrimary));
            set1.setHighlightEnabled(true);

            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(set1);

            BarData data = new BarData(dataSets);
            data.setValueTextSize(10f);
            data.setValueTextColor(getResources().getColor(R.color.colorPrimary));
            data.setDrawValues(false);
            data.setBarWidth(0.9f);
            data.setHighlightEnabled(true);

            bcDianliang.setData(data);
        }
    }

    /**
     * 初始化折线图控件属性
     */
    private void initLineChart() {
        lcDianliang.getDescription().setEnabled(false);
//        Description description=new Description();
//        description.setText("日用电量对比");
//        lcDianliang.setDescription(description);
        lcDianliang.setDoubleTapToZoomEnabled(false);
        lcDianliang.setScaleEnabled(false);
        lcDianliang.setBackgroundColor(getResources().getColor(R.color.white));
        lcDianliang.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, Highlight h) {
                lcDianliang.getXAxis().removeAllLimitLines();
                LimitLine xLimitLine = new LimitLine(e.getX(),"");
                xLimitLine.setLineColor(getResources().getColor(R.color.colorPrimary));
                xLimitLine.setTextColor(getResources().getColor(R.color.colorPrimary));
                lcDianliang.getXAxis().addLimitLine(xLimitLine);
            }

            @Override
            public void onNothingSelected() {
                lcDianliang.getXAxis().removeAllLimitLines();
            }
        });

        ValueFormatter dayValueFormatter=new DayValueFormatter();
        XAxis xAxis = lcDianliang.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setGranularity(1f);
        xAxis.setDrawGridLines(false);
        xAxis.setLabelCount(25,false);
        xAxis.setValueFormatter(dayValueFormatter);


        ValueFormatter yValueFormatter=new YValueFormatter();
        YAxis leftAxis = lcDianliang.getAxisLeft();
        leftAxis.setLabelCount(6, false);
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setSpaceTop(16f);
        leftAxis.setAxisMinimum(0f);
        leftAxis.setValueFormatter(yValueFormatter);

        lcDianliang.getAxisRight().setEnabled(false);

        XYMarkerView xyMarkerView=new XYMarkerView(this);
        xyMarkerView.setChartView(lcDianliang);
        lcDianliang.setMarker(xyMarkerView);

        setLineChartData();
    }

    /**
     * 设置折线图的数据
     */
    private void setLineChartData() {
        //填充数据，在这里换成自己的数据源
        List<Entry> valsComp1 = new ArrayList<>();
        List<Entry> valsComp2 = new ArrayList<>();

        for (int i=1;i<32;i++){
            valsComp1.add(new Entry(i,  (float) Math.random()*5+i));
            valsComp2.add(new Entry(i,  (float) Math.random()*5+i));
        }

        //这里，每重新new一个LineDataSet，相当于重新画一组折线
        //每一个LineDataSet相当于一组折线。比如:这里有两个LineDataSet：setComp1，setComp2。
        //则在图像上会有两条折线图，分别表示公司1 和 公司2 的情况.还可以设置更多
        LineDataSet setComp1 = new LineDataSet(valsComp1, "2019年7月");
        setComp1.setAxisDependency(YAxis.AxisDependency.LEFT);
        setComp1.setColor(getResources().getColor(R.color.colorPrimary));
        setComp1.setDrawCircles(true);
        setComp1.setCircleColor(getResources().getColor(R.color.colorPrimary));
        setComp1.setMode(LineDataSet.Mode.LINEAR);
        setComp1.setDrawFilled(true);
        setComp1.setFillColor(getResources().getColor(R.color.colorPrimary));
        setComp1.setDrawCircleHole(true);
        setComp1.setCircleHoleColor(getResources().getColor(R.color.colorPrimary));
        setComp1.setHighLightColor(getResources().getColor(R.color.tra)); // 设置点击某个点时，横竖两条线的颜色
        setComp1.setValueTextColor(getResources().getColor(R.color.colorPrimary));
        setComp1.setValueTextSize(10f);

        LineDataSet setComp2 = new LineDataSet(valsComp2, "2019年8月");
        setComp2.setAxisDependency(YAxis.AxisDependency.LEFT);
        setComp2.setColor(getResources().getColor(R.color.colorAccent));
        setComp2.setDrawCircles(true);
        setComp2.setCircleColor(getResources().getColor(R.color.colorAccent));
        setComp2.setMode(LineDataSet.Mode.LINEAR);
        setComp2.setDrawFilled(true);
        setComp2.setFillColor(getResources().getColor(R.color.colorAccent));
        setComp2.setDrawCircleHole(true);
        setComp2.setCircleHoleColor(getResources().getColor(R.color.colorAccent));
        setComp2.setHighLightColor(getResources().getColor(R.color.tra)); // 设置点击某个点时，横竖两条线的颜色
        setComp2.setValueTextColor(getResources().getColor(R.color.colorAccent));
        setComp2.setValueTextSize(10f);

        List<ILineDataSet> dataSets = new ArrayList<>();
        dataSets.add(setComp1);
        dataSets.add(setComp2);

        LineData lineData = new LineData(dataSets);
        lineData.setDrawValues(false);
        lineData.setHighlightEnabled(true);

        lcDianliang.setData(lineData);
        lcDianliang.invalidate();
    }

}
